import React, { Component } from "react"
import store from "../redux/store"
export default class Count extends Component {
//   componentDidMount() {
//     store.subscribe(() => {
//        // 检测store状态变化 更新页面
//       this.setState({})
//     })
//   }
  // 加法
  adds = () => {
    const { value } = this.seleceNumber
    store.dispatch({ type: "increment", data: value * 1 })
  }

  // 减法
  decs = () => {
    const { value } = this.seleceNumber
    store.dispatch({ type: "decrement", data: value * 1 })
  }

  // 奇数再加
  ifOdd = () => {
    const { value } = this.seleceNumber
    const count = store.getState()
    if (count % 2 !== 0) {
      store.dispatch({ type: "increment", data: value * 1 })
    }
  }
  // 异步加
  AsyncAdd = () => {
    const { value } = this.seleceNumber
    setTimeout(() => {
      store.dispatch({ type: "increment", data: value * 1 })
    }, 500)
  }
  render() {
    return (
      <>
        <h1>当前值为：{store.getState()}</h1>&nbsp;&nbsp;
        <select ref={(c) => (this.seleceNumber = c)}>
          <option value="1">1</option>
          <option value="2">2</option>
          <option value="3">3</option>
        </select>
        &nbsp;&nbsp;
        <button onClick={this.adds}>+</button>&nbsp;&nbsp;
        <button onClick={this.decs}>-</button>&nbsp;&nbsp;
        <button onClick={this.ifOdd}>当前求和为奇数再加</button>&nbsp;&nbsp;
        <button onClick={this.AsyncAdd}>异步加</button>
      </>
    )
  }
}
